package com.fridge.util;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.fridge.data.DBHelper;
import com.fridge.data.FridgeItem;
import com.fridge.ui.MainActivity;
import com.id2216.fridge.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;

public class AlarmNotification extends BroadcastReceiver  {

		int day = 2;
		DBHelper db = null;
		 @Override
		 public void onReceive(Context context, Intent intent) {
			
	         SharedPreferences prefs =  context.getSharedPreferences("smartFridgePrefs",Context.MODE_PRIVATE);   
	         day = prefs.getInt("notificationDay", 2);
	         String allowNo = prefs.getString("allowNotification", "YES");

	         if( allowNo.compareTo("YES") == 0)
	         {
				 Date dt = new Date();
				 Calendar c = Calendar.getInstance(); 
				 c.setTime(dt); 
				 c.add(Calendar.DATE, day);
				 dt = c.getTime();
				 
				 String imagePath = null;

				 if( db == null)
					 db = new DBHelper(context);
				 
				 List<FridgeItem> all = db.getAllFridgeItem();
				 if( all.size() > 0)
				 {
					 NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
					 inboxStyle.setBigContentTitle("Fridge Items");
					 int counter = 0;
					 for(int i = 0 ; i < all.size() ; i++)
					 {
						 FridgeItem item = all.get(i);
						 if( item.getExpireDayCount() <= day )
						 {	
							 
							inboxStyle.addLine(item.getName() + " : " + item.getExpireDay()) ;
						 	counter++;
						 	
						 	if(item.getPic().compareTo("") != 0)
						 		imagePath = item.getPic();
						 }
					 }
					 
					 // Notification 
					 Bitmap bigIcon = null;
					 

 // Creates notification object
 NotificationCompat.Builder mBuilder =  new NotificationCompat.Builder(this);
 mBuilder.setSmallIcon(R.drawable.ic_launcher);
 mBuilder.setContentTitle("Your food will expire soon");
 mBuilder.setContentText("Banana");

 // Creates an explicit intent for an Activity in your app
 Intent resultIntent = new Intent(context, MainActivity.class);
 TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
 stackBuilder.addParentStack(MainActivity.class);
 stackBuilder.addNextIntent(resultIntent);
 PendingIntent resultPendingIntent =
                     stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT );
 
 mBuilder.setContentIntent(resultPendingIntent);
 mBuilder.setStyle(inboxStyle);
 
 // Get notification mananger
 NotificationManager notificationMgr =
                     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

 // Issue notification
 int notificationID = 1;
 notificationMgr.notify(notificationID, mBuilder.build());
otifyMgr.cancel(NOTIFY_ME_ID);
                     }
                     
                     
//					 if(imagePath != null)
//					 {
//				        	BitmapFactory.Options options = new BitmapFactory.Options();
//				        	options.inSampleSize = 8;
//				        	try
//				        	{
//				        		bigIcon = BitmapFactory.decodeFile(imagePath, options);  
//				        	}
//				        	catch(Exception e)
//				        	{
//				        		
//				        	}
//				        	if(bigIcon != null)
//				        		mBuilder.setLargeIcon(bigIcon);
//					 }
					 
					 
//					 mBuilder.setContentText( Integer.toString(counter) + " items");
//					 // Creates an explicit intent for an Activity in your app
//					 Intent resultIntent = new Intent(context, MainActivity.class);
//					 TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
//					 stackBuilder.addParentStack(MainActivity.class);
//					 stackBuilder.addNextIntent(resultIntent);
//					 PendingIntent resultPendingIntent =
//				    	        stackBuilder.getPendingIntent(
//				    	            0,
//				    	            PendingIntent.FLAG_UPDATE_CURRENT
//				    	        );
//				    	
//					 mBuilder.setContentIntent(resultPendingIntent);  
//					 mBuilder.setStyle(inboxStyle);
//
//					 NotificationManager mNotificationManager =  (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//					 mNotificationManager.notify(1, mBuilder.build());
					 
				 }
	        	 
	         }
	         
			 
		}

}
